//
//  LZNotSubmitCouponCell.m
//  laziz_Merchant
//
//  Created by xbnzd on 16/12/20.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZPublisedCouponCell.h"
#import "LZCouponListModel.h"
#import "UIImageView+WebCache.h"
#import "UIView+Rotation.h"

@interface LZPublisedCouponCell()
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *rightArrowImg;
@property (weak, nonatomic) IBOutlet UILabel *topRightLabel;
@property (weak, nonatomic) IBOutlet UIImageView *vidoeCoverImgView;
@property (weak, nonatomic) IBOutlet UILabel *viewsCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *zanCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *collectCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *msgCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *couponCountLabel;
@property (weak, nonatomic) IBOutlet UIView *vLine;
@property (weak, nonatomic) IBOutlet UIImageView *couponIcon;
@property (weak, nonatomic) IBOutlet UIImageView *groupIcon;
@property (weak, nonatomic) IBOutlet UILabel *groupCountLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *finishBtnWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *couponLabelCenterXConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *groupLabelCenterXConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *groupLabelLeadingToSuperView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *couponLabelLeadingToSuperView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *finshBtnTopToPlayBgViewConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *couponLabelTopToPlayBgViewConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *couponLabelCenterYToFinishBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *groupLabelTopToPlayBgViewConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *groupLabelCenterYToFinishBtn;
@property (weak, nonatomic) IBOutlet UIButton *finishBtn;
@property (strong, nonatomic) LZCouponListModel *dataModel;
@property (nonatomic, strong) NSIndexPath *indexP;

@end

@implementation LZPublisedCouponCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
     _finishBtnWidth.constant = (SCREEN_WIDTH - 60)/3;
    [self configureBtnLayer:self.finishBtn];
    [_finishBtn setTitle:LZLocalizedString(@"finish", nil) forState:UIControlStateNormal];
    
    [self.rightArrowImg setLanguageRotaion];
    
    self.vidoeCoverImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playAction)];
    [self.vidoeCoverImgView addGestureRecognizer:tap];
    
    self.couponCountLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *couponTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCouponAction)];
    [self.couponCountLabel addGestureRecognizer:couponTap];
    
    self.groupCountLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *groupTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickGroupAction)];
    [self.groupCountLabel addGestureRecognizer:groupTap];
    
}

#pragma mark -
#pragma mark public
- (void)loadContentWithModel:(LZCouponListModel *)dataModel indexPath:(NSIndexPath *)indexPath
{
    self.dataModel = dataModel;
    self.indexP = indexPath;
    
    self.nameLabel.text = dataModel.promotionTitle;
    
    [self.vidoeCoverImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",APIBaseImageUrl,dataModel.videoPhoto]] placeholderImage:[UIImage imageNamed:@"videoCoverImg"]];
    
    self.viewsCountLabel.text = [NSString stringWithFormat:@"%d",dataModel.playCount];
    self.zanCountLabel.text = [NSString stringWithFormat:@"%d",dataModel.likesCount];
    self.collectCountLabel.text = [NSString stringWithFormat:@"%d",dataModel.storeCount];
    self.msgCountLabel.text = [NSString stringWithFormat:@"%d",dataModel.remarkCountPerson];
    
    //==
    [self setCouponAndGroupContentWith:dataModel];
    
    [self setCouponAndGroupAndFootBtnFrameWith:dataModel];
   
}

#pragma mark -
#pragma mark btn action
- (void)clickCouponAction
{
    if (self.publishCellDelegate && [self.publishCellDelegate respondsToSelector:@selector(clickDetailOrPlayVideoBtnWithIndexPath:couponBtnCode: withCell:)]) {
        [self.publishCellDelegate clickDetailOrPlayVideoBtnWithIndexPath:self.indexP couponBtnCode:kCouponList_couponDetail withCell:self];
    }
}

- (void)clickGroupAction
{
    if (self.publishCellDelegate && [self.publishCellDelegate respondsToSelector:@selector(clickDetailOrPlayVideoBtnWithIndexPath:couponBtnCode: withCell:)]) {
        [self.publishCellDelegate clickDetailOrPlayVideoBtnWithIndexPath:self.indexP couponBtnCode:kCouponList_groupDetail withCell:self];
    }
}

- (void)playAction
{
    if (self.publishCellDelegate && [self.publishCellDelegate respondsToSelector:@selector(clickDetailOrPlayVideoBtnWithIndexPath:couponBtnCode: withCell:)]) {
        [self.publishCellDelegate clickDetailOrPlayVideoBtnWithIndexPath:self.indexP couponBtnCode:kCouponList_playVideo withCell:self];
    }
}
- (IBAction)topRightBtnAction:(UIButton *)sender {
    
    if (self.publishCellDelegate && [self.publishCellDelegate respondsToSelector:@selector(clickDetailOrPlayVideoBtnWithIndexPath:couponBtnCode: withCell:)]) {
        [self.publishCellDelegate clickDetailOrPlayVideoBtnWithIndexPath:self.indexP couponBtnCode:kCouponList_detail withCell:self];
    }
}
- (IBAction)finishBtnAction:(UIButton *)sender {
    
    if (self.cellClickDelegate && [self.cellClickDelegate respondsToSelector:@selector(clickCellButtonWithIndex: andCell:)]) {
        [self.cellClickDelegate clickCellButtonWithIndex:kCouponList_finish andCell:self];
    }
}

#pragma mark -
#pragma mark private

/**
 赋值

 @param dataModel <#dataModel description#>
 */
- (void)setCouponAndGroupContentWith:(LZCouponListModel *)dataModel
{
    if (dataModel.couponsName.length > 0) {
        self.couponCountLabel.attributedText = [self configureTextColorWithDefaultTitle:LZLocalizedString(@"coupons", nil) UseCount:[NSString stringWithFormat:@"%d",dataModel.couponsGetCount] TotalCount:[NSString stringWithFormat:@"%d",dataModel.couponsTotalCount]];
    }
    
    if (dataModel.grouponName.length > 0) {
        self.groupCountLabel.attributedText = [self configureTextColorWithDefaultTitle:LZLocalizedString(@"group_order", nil) UseCount:[NSString stringWithFormat:@"%d",dataModel.grouponFinishCount] TotalCount:@""];
    }
   
}


/**
 配置 优惠券, 团购 按钮的位置

 @param dataModel <#dataModel description#>
 */
- (void)setCouponAndGroupAndFootBtnFrameWith:(LZCouponListModel *)dataModel
{
    if (dataModel.couponsName.length > 0 && dataModel.grouponName.length == 0) {//只有优惠券
        self.vLine.hidden = YES;
        self.groupIcon.hidden = YES;
        self.groupCountLabel.hidden = YES;
        self.couponIcon.hidden = NO;
        self.couponCountLabel.hidden = NO;
        
        self.couponLabelCenterXConstraint.priority = 250;
        self.couponLabelLeadingToSuperView.priority = 999;
        self.finshBtnTopToPlayBgViewConstraint.constant = 15;
        self.couponLabelTopToPlayBgViewConstraint.priority = 250;
        self.couponLabelCenterYToFinishBtn.priority = 999;
        
    }else if (dataModel.couponsName.length == 0 && dataModel.grouponName.length > 0){//只有团购
        self.vLine.hidden = YES;
        self.groupIcon.hidden = NO;
        self.groupCountLabel.hidden = NO;
        self.couponIcon.hidden = YES;
        self.couponCountLabel.hidden = YES;

        self.groupLabelCenterXConstraint.priority = 250;
        self.groupLabelLeadingToSuperView.priority = 999;
        self.finshBtnTopToPlayBgViewConstraint.constant = 15;
        self.groupLabelCenterYToFinishBtn.priority = 999;
        self.groupLabelTopToPlayBgViewConstraint.priority = 250;
    }else if (dataModel.couponsName.length == 0 && dataModel.grouponName.length == 0){//两个都没有
        self.vLine.hidden = YES;
        self.groupIcon.hidden = YES;
        self.groupCountLabel.hidden = YES;
        self.couponIcon.hidden = YES;
        self.couponCountLabel.hidden = YES;

        self.finshBtnTopToPlayBgViewConstraint.constant = 15;
        
    }else if (dataModel.couponsName.length > 0 && dataModel.grouponName.length > 0){//两个都有
        self.vLine.hidden = NO;
        self.groupIcon.hidden = NO;
        self.groupCountLabel.hidden = NO;
        self.couponIcon.hidden = NO;
        self.couponCountLabel.hidden = NO;

        self.groupLabelCenterXConstraint.priority = 999;
        self.groupLabelLeadingToSuperView.priority = 250;
        
        self.couponLabelCenterXConstraint.priority = 999;
        self.couponLabelLeadingToSuperView.priority = 250;
        
        self.finshBtnTopToPlayBgViewConstraint.constant = 45.5;
        
        self.couponLabelCenterYToFinishBtn.priority = 250;
        self.groupLabelCenterYToFinishBtn.priority = 250;
        self.couponLabelTopToPlayBgViewConstraint.priority = 999;
        self.groupLabelTopToPlayBgViewConstraint.priority = 999;
    }
}

- (void)configureBtnLayer:(UIButton *)btn
{
    btn.layer.borderColor = UIColorFromRGB(0xcccccc, 1).CGColor;
    btn.layer.borderWidth = 0.5;
    btn.layer.cornerRadius = 5;
    btn.layer.masksToBounds = YES;
}

- (NSMutableAttributedString *)configureTextColorWithDefaultTitle:(NSString *)defaultTitle UseCount:(NSString *)useCount TotalCount:(NSString *)totalCount
{
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@%@%@",defaultTitle,useCount,totalCount.length == 0 ? @"": @"/",totalCount]];
    [attStr addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x36bb2a, 1) range:NSMakeRange(defaultTitle.length + 1, useCount.length)];
    
    return attStr;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
